<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/10
 * Time: 21:59
 */

namespace app\lnadmin\model\admin;

use dateset\Dateset;
use think\facade\Session;
use app\lnadmin\model\core\InitModel;

/**
 * 点赞记录
 * Class ViewLog
 * @package app\model\admin
 */
class OnlineUsers extends InitModel
{
    // 定义表
    protected $name = 'online_users';
 
   // 在登录或访问时的处理
public static function onlineUserTracking()
{   
    $sessionId = Session::getId();
    $data = self::whereIn('session_id', $sessionId)->value('session_id');
        // 游客
    if ($sessionId == $data) {
        // 插入或更新用户在线记录
        self::whereIn('session_id', $sessionId)->update(['last_active_time' => time()]);
    } else {
        $data = [
           'user_name' => \app\store\controller\Index::cars()['car'],
           'session_id' => $sessionId,
           'last_active_time' => time()
        ];
        self::create($data);
   }
}

// 定时任务删除离线用户
public static function removeOfflineUsers()
{
    $timeout = time() - 7200; // 120 分钟
    self::whereTime('last_active_time',  '<=', $timeout)->delete();
}

// 获取在线人数
public static function getOnlineCount()
{
    return self::count();
}

}